package ywpt.service.client;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientInfo;
import ywpt.model.ClientVisit;
import ywpt.model.JobInfo;
import ywpt.model.RepairInfo;
import ywpt.web.common.PublicFunction;

public class ClientVisitServiceImpl extends HibernateEntityDao<ClientVisit> implements ClientVisitService{
	public ClientVisitServiceImpl(){
		
	}

	@Override
	public String createClientVisitCode() {
		SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMdd");
		DetachedCriteria query = DetachedCriteria.forClass(ClientVisit.class, "visit");
		query.add(Restrictions.like("code",FORMAT.format(new Date())+"%"));
		String visitCode="";
		String code="";
		Integer maxId=new Integer(0); 
		query.addOrder(Order.desc("id"));
		List<ClientVisit> clientVisits = this.getClientVisits(query);
		if ( clientVisits.iterator().hasNext() )	
		{
			code = (clientVisits.iterator().next().getCode()).substring(8);
			maxId = new Integer(code);
		}
		visitCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return FORMAT.format(new Date())+visitCode;
	}

	@Override
	public ClientVisit getClientVisitById(Long id) {
		return this.get(id);
	}

	@Override
	public int getClientVisitCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientVisit> getClientVisits(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ClientVisit> getClientVisits() {
		return this.getAll();
	}

	@Override
	public void removeClientVisit(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveClientVisit(ClientVisit clientVisit,Long jobId, Long repairId, Long clientId) {
		if (jobId!=null) {
			clientVisit.setJobInfo(this.get(JobInfo.class, jobId));
			clientVisit.setJobName(this.get(JobInfo.class, jobId).getJobCode());
		}
		if (repairId!=null) {
			clientVisit.setRepair(this.get(RepairInfo.class, repairId));
		}
		if (clientId!=null) {
			clientVisit.setClient(this.get(ClientInfo.class, clientId));
		}
		super.save(clientVisit);
	}

	@Override
	public void updatClientVisit(ClientVisit clientVisit,Long jobId, Long repairId, Long clientId) {
		if (jobId!=null) {
			clientVisit.setJobInfo(this.get(JobInfo.class, jobId));
			clientVisit.setJobName(this.get(JobInfo.class, jobId).getJobCode());
		}
		if (repairId!=null) {
			clientVisit.setRepair(this.get(RepairInfo.class, repairId));
		}
		if (clientId!=null) {
			clientVisit.setClient(this.get(ClientInfo.class, clientId));
		}
		super.update(clientVisit);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientVisit> getClientVisits(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}
}
